//Ajax request function
function GetXmlHttpObject()
{
var xmlHttp=null;
try
  {
  // Firefox, Opera 8.0+, Safari
  xmlHttp=new XMLHttpRequest();
  }
catch (e)
  {
  // Internet Explorer
  try
    {
    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
  catch (e)
    {
    xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
  }
return xmlHttp;
}

//Ajax call for state list 
function funSelectState(val,divId){
	 document.getElementById(divId).innerHTML="<img src='../images/spinningLoader.gif'> Please wait..";
	 xmlHttp = GetXmlHttpObject();  //create object for http request
	 if (xmlHttp==null)  {
		  alert ("Your browser does not support AJAX!");
		  return;
	 }	  
	 var url="../state_list.php"
	 url=url+"?id="+val	 
	 xmlHttp.open("GET", url, true);
	 xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	 xmlHttp.send(null);	 
	 xmlHttp.onreadystatechange = function() {
		if (xmlHttp.readyState==4) { 
		document.getElementById(divId).innerHTML=xmlHttp.responseText;
		}
	 }	 	
}
//Ajax call for city list 
function funSelectCity(val,divId){
	 document.getElementById(divId).innerHTML="<img src='../images/spinningLoader.gif'> Please wait..";
	 xmlHttp = GetXmlHttpObject();  //create object for http request
	 if (xmlHttp==null)  {
		  alert ("Your browser does not support AJAX!");
		  return;
	 }	  
	 var url="../city_list.php"
	 url=url+"?id="+val	 
	 xmlHttp.open("GET", url, true);
	 xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	 xmlHttp.send(null);		 
	 xmlHttp.onreadystatechange = function() {
		if (xmlHttp.readyState==4) { 
		document.getElementById(divId).innerHTML=xmlHttp.responseText;
		}
	 }	 	
}

//Ajax call for city list 
function getUsers(){
	document.getElementById('utype').value=document.getElementById('selType').value;
	 document.getElementById('loading').innerHTML="<img src='../images/spinningLoader.gif'> Please wait..";
	 xmlHttp = GetXmlHttpObject();  //create object for http request
	 if (xmlHttp==null)  {
		  alert ("Your browser does not support AJAX!");
		  return;
	 }	  
	 var url="userlist.php"
	 url=url+"?id="+document.getElementById('selType').value	 
	 xmlHttp.open("GET", url, true);
	 xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	 xmlHttp.send(null);		 
	 xmlHttp.onreadystatechange = function() {
		if (xmlHttp.readyState==4) { 
		document.getElementById("loading").innerHTML=xmlHttp.responseText;
		}
	 }	 	
}

function logOffAdmin()
 {
  	var x=document.getElementById("formLogOff");
  	document.getElementById("logoff").value="logoff";
  	x.submit();
 }
function SignInSubmit()
 {
  	var x=document.getElementById("form1");
  	document.getElementById("keyword").value="keyword";
  	x.submit();
 }
  
function CategorySubmit()
 {  
  	var x=document.getElementById("formCategory");
  	//document.getElementById("CategoryKey").value="add";
  	if(validateCategory())
  	x.submit();
 }
function validateCategory()
 {
     var frm = document.formCategory;	
	 
	 if(frm.cat_name.value == '')
	 {
	 	alert("Enter Category Name");
		var cat_name=0;
		frm.cat_name.focus();
		return false;
	 }
	 else
	    var cat_name=1;
		if(frm.cat_description.value == '')
	 	 {
			alert("Enter Category Description");
			var cat_description=0;
			frm.cat_description.focus();
			return false;
	 	 }
	    else
	    	var cat_description=1;
		
  if(cat_name==1 && cat_description==1)
   {
	   return true;
   }
 }
 
 
function editCatSubmit()
 {
  	var x=document.getElementById("formCategory");
  	document.getElementById("CategoryKey").value="edit";
  	if(validateCategory())
  	x.submit();
 }


function editCatLink(cat)
 {
	window.location="index.php?cat&edit="+cat;
 }
//Delete Category
function deleteCatSubmit(catId)
 {
  	var delCat=confirm("Are you sure want to delete Category?");
  	if(delCat==true)
  	 {
	  	var x=document.getElementById("formCategory");	
	  	document.getElementById("delCategory").value=catId;
	  	x.submit();
  	 }
 }
 
 
 
 
 
function SubCategorySubmit()
 {  
	var x=document.getElementById("formSubCategory");
  	document.getElementById("SubCategoryKey").value="SubCategoryKey";
  	if(validateSubCategory())
  	x.submit();
 }
function validateSubCategory()
 {
     var frm = document.formSubCategory;
	 if(frm.cat_name.value == 'no')
	  {
	 	alert("Choose Category");
		var cat_name=0;
		frm.cat_name.focus();
		return false;
	  }
	 else
	    var cat_name=1;
	 if(frm.sub_cat_name.value == '')
	 {
	 	alert("Enter Subcategory Name");
		var sub_cat_name=0;
		frm.sub_cat_name.focus();
		return false;
	 }
	 else
	    var sub_cat_name=1;
	if(frm.subcat_description.value == '')
	 {
		alert("Enter Subcategory Description");
		var subcat_description=0;
		frm.subcat_description.focus();
		return false;
	 }
	else
	   var subcat_description=1;

  if(cat_name==1 && sub_cat_name==1 && subcat_description==1)
   {
	   return true;
   }
 }
function categoryListSubmit(val)
 { 
	 window.location="index.php?listscat&catId="+val;
 }
function editSubategory(subcat)
 {
	window.location="index.php?editscat&esubcat="+subcat;
 }
function editSubCatsubmit()
{
  var x=document.getElementById("formEditSubCat");
  document.getElementById("CategoryKey").value="CategoryKey";
  if(validateEditSubCat())
  x.submit();
}
function validateEditSubCat()
 {
     var frm = document.formEditSubCat;
	 if(frm.cat_name.value == '')
	 {
	 	alert("Enter Category Name");
		var cat_name=0;
		frm.cat_name.focus();
		return false;
	 }
	 else
	    var cat_name=1;
		if(frm.cat_description.value == '')
	 {
	 	alert("Enter Category Description");
		var cat_description=0;
		frm.cat_description.focus();
		return false;
	 }
	 else
	    var cat_description=1;
		
  if(cat_name==1 && cat_description==1)
   {
	  return true;
   }
 }
function deleteSubCategory(catId)
{
  var delCat=confirm("Are you sure want to delete SubCategory?");
  if(delCat==true)
  {
	  var x=document.getElementById("formDelCategory");
	  document.getElementById("delSubCategory").value=catId;
	  x.submit();
  }
} 
function changePassword()
 {
  	var x=document.getElementById("form1");
  	document.getElementById("chgPwdKey").value="chgPwdKey";
   	if(validateChangePwd())
     x.submit();
 }
function validateChangePwd()
 {
  var oldPwd=document.getElementById("oldPassword").value;
  var newPwd=document.getElementById("newPassword").value;
  var cfrmPwd=document.getElementById("cfrmNewPassword").value;
  submitOK="true";
  if (oldPwd=='')
  {
	 alert("Enter Old Password");
	 submitOK="false";
	 document.getElementById("oldPassword").focus();
	 return false;
  }
 if (newPwd=='')
  {
	 alert("Enter New Password");
	 submitOK="false";
	 document.getElementById("newPassword").focus();
	  return false;
  }
 if (newPwd!=cfrmPwd)
  {
	 document.getElementById("newPassword").value='';
	 document.getElementById("cfrmNewPassword").value='';
	 alert("New Password and Confirm password did'nt match");
	 submitOK="false";
	 document.getElementById("oldPassword").focus();
	 return false;
  }
 if (submitOK=="true")
  {
 	return true;
  }
}

//add class to field
function addClass(element, value) {
	if(!element.className) {
		element.className = value;
	} else {
		newClassName = element.className;
		newClassName+= " ";
		newClassName+= value;
		element.className = newClassName;
	}
}

function removeClass(element) {
	if(element.className) {
		element.className="";
	}
}

function funEditProfile() {
	var frm = document.frmEditProfile;	
	
	if(document.getElementById("firstName").value=="") {		
		fname=1;
		addClass(document.getElementById('lblFirstName'), 'errorField');
	}
	else {
		fname=0;
		removeClass(document.getElementById('lblFirstName'));
	}
	
	if(document.getElementById("lastName").value=="") {		
		lname=1;
		addClass(document.getElementById('lblLastName'), 'errorField');
	}
	else {
		lname=0;
		removeClass(document.getElementById('lblLastName'));
	}
	
	if(document.getElementById("currentPass").value=="") {		
		cpass=1;
		addClass(document.getElementById('lblCurPass'), 'errorField');
	}
	else {
		cpass=0;
		removeClass(document.getElementById('lblCurPass'));
	}
	
	
	
	if(document.getElementById("address1").value=="") {		
		add1=1;
		addClass(document.getElementById('lblAddress1'), 'errorField');
	}
	else {
		add1=0;
		removeClass(document.getElementById('lblAddress1'));
	}
	
	if(frm.city.value==""){
		ucity=1;
		addClass(document.getElementById('lblCity'), 'errorField');	
	}
	else {
		ucity=0;
		removeClass(document.getElementById('lblCity'));
	}
	
	if(frm.country.value==""){
		cnty=1;
		addClass(document.getElementById('lblCountry'), 'errorField');		
	}
	else{
		cnty=0;
		removeClass(document.getElementById('lblCountry'));
	}
	
	if(frm.state.value==""){
		st=1;
		addClass(document.getElementById('lblState'), 'errorField');		
	}
	else{
		st=0;
		removeClass(document.getElementById('lblState'));
	}
	
	if(frm.zip.value==""){
		uzip=1;
		addClass(document.getElementById('lblZip'), 'errorField');	
	}
	else{
		uzip=0;
		removeClass(document.getElementById('lblZip'));
	}
	
	if(frm.phoneNumber1.value=="" || frm.phoneNumber2.value=="" || frm.phoneNumber3.value=="" || !Number(frm.phoneNumber1.value) || !Number(frm.phoneNumber2.value) || !Number(frm.phoneNumber3.value)){
		uphone=1;
		addClass(document.getElementById('lblPhoneNumber'), 'errorField');	
	}
	else{
		uphone=0;
		removeClass(document.getElementById('lblPhoneNumber'));
	}
		
	if(frm.email.value==""){
		mail=1;
		addClass(document.getElementById('lblEmail'), 'errorField');	
	}else { 
		if(!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(frm.email.value))){				
			mail=1;
			addClass(document.getElementById('lblEmail'), 'errorField');	
		}		
	}
	
	//alert(err);
	
	if(fname==1 || lname==1 || cpass==1 || add1==1 || ucity==1 || cnty==1 || st==1 || uphone==1 || mail==1 || uzip==1) {
		//alert("hai");
		document.getElementById("messageArea").innerHTML="<font color='#FF0000'><b>Enter valid information in red color highlighted fields.</b></font>";
		return false;
	}
	else {
		return true;
	}
}


//old functions not needed


function checkAdsPhoto8(val)
 {
		
		type=val.split(".");
		//alert(type[1]);
		ftype=type[1].toLowerCase();
		if(ftype!="gif" && ftype!="png" && ftype!="jpg" && ftype!="jpeg")
		{
			alert("Please upload gif,png,jpg type only");			
			document.getElementById("photo8").value='';
			document.getElementById("save").disabled=true;
			return false;
		}
		else { document.getElementById("save").disabled=false; }
 }